#include "ServiceConnector.h"

#include <string>
#include <stdio.h>
//#include <boost/system/system_error.hpp>
#include "unistd.h"

namespace Core { namespace EndUserAPI {

const int MaxCountMessage = 5;

ServiceConnector::ServiceConnector(const char *hostPort)
    : m_host(hostPort)
{
}

ServiceConnector::~ServiceConnector()
{
}

void ServiceConnector::sendSearchMessage(const char *filename) throw (ServiceConnectorError)
{
    initDebug();
}

void ServiceConnector::sendSearchMessage(char *buf, std::size_t bufLen) throw (ServiceConnectorError)
{
    initDebug();
}

void ServiceConnector::establishConnection() throw (ServiceConnectorError)
{
}

IMessage *ServiceConnector::getNextMessage()
{
    usleep(100000);
    if (++mCountMessage < mMaxMessage)
    {
        IMessage *progressMsg = new ProgressMessage(mCountMessage / mMaxMessage);
        return progressMsg;

    }
    else
    {
        return messageResult;
    }
}

void ServiceConnector::close()
{
}

void ServiceConnector::initDebug()
{
    mCountMessage = 0;
    mMaxMessage = MaxCountMessage;//(rand() * MaxCountMessage) + 1;

    std::string str;
    str = "{ab306a70-0fd2-4b4b-84fe-f9ebd899d90f}";
    messageResult = new GUIDFoundMessage(str);

/*
    switch (int(rand() * 4))
    {
        case 0 : str = "{ab306a70-0fd2-4b4b-84fe-f9ebd899d90f}";
                 messageResult = new GUIDFoundMessage(str);
        case 1 : messageResult = new NotFoundMessage();
        case 2 : str = "test service connector";
                 messageResult = new ErrorMessage(1, str);
        case 3 : messageResult = new InterruptedMessage();
    }
*/
}

} } // namespace Core, EndUserAPI
